package com.lucian.ge.controller.shopadmin;

import com.lucian.ge.dao.shop.AdminMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * Created by Lucicol on 2019/8/8.
 */
@Api(value = "TimingTaskController", description = "TimingTask-定时任务")

@RestController
@RequestMapping("/manage/timingtask")
public class TimingTaskController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired(required=false)
    private AdminMapper adminMapper;

    @ApiOperation(value="超时未支付订单清理", notes="")
    @GetMapping("/notpay")
    public void test() {

        String status = adminMapper.getChangeNotPay();
        System.out.println("status:" + status);
        // 一小时一次清理
        Integer cacheTime = 1000 * 60 * 60;
        Timer timer = new Timer();

        if(!status.equals("1")) {
            adminMapper.changeNotPay();

            // (TimerTask task, long delay, long period)任务，延迟时间，多久执行
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
//                    System.out.println(new Date());
                    System.out.println("payTimeout");
                    adminMapper.payTimeout();
                }
            }, 1000, cacheTime);
        }

//        return;
    }


    @ApiOperation(value="test2", notes="")
    @GetMapping("/test2")
    public void test2() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));

//        String dStr = "2001-12-12 08:23:21";
        String dStr = sdf.format(date);
        Date d = null;
        try {
            d = sdf.parse(dStr);
        } catch (ParseException pe) {
            System.out.println(pe.getMessage());
        }
        System.out.println(d);
        System.out.println(d.getTime());

        long time = d.getTime();
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        System.out.println("t:" + sd);

        long time2 = d.getTime() + 1000 * 60 * 60 * 24;
        String sd2 = sdf.format(new Date(Long.parseLong(String.valueOf(time2))));
        System.out.println("t2:" + sd2);


//        redisTemplate.opsForValue().set("notPay", "0");
//        Timer timer = new Timer();
//        timer.cancel();
//        System.exit(0);
//        return;
    }


}
